<?php

class dbBackupQueryTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));

    $this->namespace        = '';
    $this->name             = 'dbBackupQuery';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [dbBackupQuery|INFO] task does things.
Call it with:

  [php symfony dbBackupQuery|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    // add your code here

      $bpFile = new BPReader();
      $bpFile->load();
      $data = $bpFile->getData();
      foreach($data->section as $section)
      {
        $name = $section->attributes()->name;
        if($name)
        {
            $name = (string)$name;
            foreach($section->element as $element)
            {
                if(Doctrine_Core::getTable($name)->findOneBy('id', $element->attributes()->id))
                {
                    continue;
                }
                $obj = new $name();
                foreach($element->attributes() as $attr => $value)
                {
                    if($value == "")
                    {
                        continue;
                    }
                    $obj->set($attr, $value);
                }
                $obj->save();
            }

        }
      }
  }
}
